package com.github.bridge.controller;

import cn.hutool.core.io.FileUtil;
import com.github.bridge.annotation.SysLog;
import com.github.bridge.handler.ESignHandler;
import com.github.bridge.request.GeneratePdfFileDTO;
import com.github.bridge.request.PdfPreviewUrlDTO;
import com.github.bridge.request.TemplateContentDTO;
import com.github.bridge.request.UploadWithAuthDTO;
import com.github.bridge.response.*;
import com.github.bridge.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Tag(name = "文件相关")
@ApiSupport(order = 3, author = "张辉")
public class FileController {
    private final ESignHandler eSignHandler;
    private final HttpServletResponse response;
    @Value("${file.path}")
    private String filePath;

//    @SaIgnore
    @PostMapping("/generatePdfFile")
    @Operation(summary = "根据模板生成待签署文件")
    @ApiOperationSupport(order = 1, author = "jwtpermission@gmail.com")
    @SysLog(value = "根据模板生成待签署文件")
//    @Log(value = "根据模板生成待签署文件", position = Position.ENABLED, readWriteType = ReadWriteType.WRITE)
    public R<GeneratePdfFileVO> generatePdfFile(@RequestBody @Validated GeneratePdfFileDTO dto)  {
        GeneratePdfFileVO vo = eSignHandler.generatePdfFile(dto);
        return R.ok(vo);
    }

    @PostMapping("/uploadWithAuth")
    @Operation(summary = "文件直传")
    @ApiOperationSupport(order = 2)
    @SysLog(value = "文件直传")
//    @Log(value = "文件直传", position = Position.ENABLED, readWriteType = ReadWriteType.WRITE)
    public R<UploadWithAuthVO> uploadWithAuth(@RequestPart(value = "file") MultipartFile file,
                                              UploadWithAuthDTO dto) throws Exception {
        // 将MultipartFile转换成file
        UploadWithAuthVO vo = null;
        File file1 = new File(filePath, Objects.requireNonNull(file.getOriginalFilename()));
        try {
            FileUtil.writeFromStream(file.getInputStream(), file1);
            vo = eSignHandler.uploadWithAuth(file1, dto);
        } finally {
            FileUtil.del(file1);
        }
        return R.ok(vo);
    }
//
//    @SaIgnore
//    @GetMapping("/getFileStream")
//    @Operation(summary = "获取文件流")
//    @Log(value = "获取文件流", position = Position.ENABLED, readWriteType = ReadWriteType.WRITE)
//    public void getFileStream(@RequestParam("fileKey") String fileKey,
//                              @RequestParam(value = "systemId", defaultValue = "1000101") String systemId)  {
//        InputStream fileStream = eSignHandler.getFileStream(fileKey, systemId);
//        ServletUtil.write(response, fileStream);
//    }



//    @SaIgnore
    @GetMapping("/getDownloadUrl")
    @Operation(summary = "获取下载链接")
//    @Log(value = "获取下载链接", position = Position.ENABLED, readWriteType = ReadWriteType.WRITE)
    public R<GetDownloadUrlVO> getDownloadUrl(@RequestParam("fileKey") String fileKey,
                              @RequestParam(value = "systemId", defaultValue = "1000101") String systemId)  {
        GetDownloadUrlVO vo = eSignHandler.getDownloadUrl(fileKey, systemId);
        return R.ok(vo);
    }

    @PostMapping("/templateContent")
    @Operation(summary = "根据模板id获取模板内容域与签署区")
    public R<TemplateContentVO> templateContent(@RequestBody @Validated TemplateContentDTO dto)  {
        TemplateContentVO vo = eSignHandler.templateContent(dto);
        return R.ok(vo);
    }

    @PostMapping("/pdfPreviewUrl")
    @Operation(summary = "根据模板id获取预览链接")
    public R<PdfPreviewUrlVO> pdfPreviewUrl(@RequestBody @Validated PdfPreviewUrlDTO dto)  {
        return R.ok(eSignHandler.pdfPreviewUrl(dto));
    }


}
